﻿using RazorEngine;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace MvcApplication1.Extension
{
    public static class BreezeEditor 
    {

        public static MvcHtmlString CustomBreezeEditor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, object attribute)
        {

            ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);
           
            string expre = expression.ToString();
            string databind = expre.Substring(expression.ToString().IndexOf('.')+1);

            string tagname = metadata.ModelType.Name;
            var builder = new TagBuilder("input");

            switch(tagname){
                case "Boolean": 
                    builder.Attributes["type"] = "checkbox";
                    builder.Attributes["data-bind"] = "checked : " + databind;
                    break;
                default:
                    builder.Attributes["type"] = "text";
                    builder.Attributes["data-bind"] = "value : " + databind;
                    break;
            }

            //builder.Attributes["readonly"] = "true";

            //Add attributes
            foreach (PropertyInfo propertyInfo in attribute.GetType().GetProperties())
            {
                if (propertyInfo.CanRead)
                {
                    string nameOfProperty = propertyInfo.Name;
                    var property = attribute.GetType().GetProperty(nameOfProperty);
                    var value = property.GetValue(attribute, null);
                    builder.Attributes[nameOfProperty] = value.ToString();
                }
            }
            
            MvcHtmlString returnStr = MvcHtmlString.Create(builder.ToString(TagRenderMode.SelfClosing));
            return returnStr;
        }

        public static MvcHtmlString CustomBreezeEditor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression)
        {

            ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);
            string expre = expression.ToString();
            string databind = expre.Substring(expression.ToString().LastIndexOf('.') + 1);

            string tagname = metadata.ModelType.Name;

            var builder = new TagBuilder("input");

            switch (tagname)
            {
                case "Boolean":
                    builder.Attributes["type"] = "checkbox";
                    builder.Attributes["data-bind"] = "checked : " + databind;
                    break;
                default:
                    builder.Attributes["type"] = "text";
                    builder.Attributes["data-bind"] = "value : " + databind;
                    break;
            }


            MvcHtmlString returnStr = MvcHtmlString.Create(builder.ToString(TagRenderMode.SelfClosing) + Insert_item_Button());
            
            return returnStr;
        }

        public static MvcHtmlString Insert_item_Button()
        {
            string str = "<button onclick=addItem()>+</button>";

            return new MvcHtmlString(str);
        }

    }
}